package dk.itu.samd.group2.tagapastry.util;

import java.io.IOException;

import dk.itu.samd.group2.tagapastry.messages.IndexExpansionConfirmationMsg;
import dk.itu.samd.group2.tagapastry.messages.IndexExpansionMsg;
import dk.itu.samd.group2.tagapastry.messages.IndexRetractionRequestMsg;
import dk.itu.samd.group2.tagapastry.messages.IndexRetractionResponseMsg;
import dk.itu.samd.group2.tagapastry.messages.ReplicationMsg;
import dk.itu.samd.group2.tagapastry.messages.ResultsMsg;
import dk.itu.samd.group2.tagapastry.messages.SearchMsg;
import dk.itu.samd.group2.tagapastry.messages.TagMsg;
import dk.itu.samd.group2.tagapastry.messages.TagapastryMsg;
import dk.itu.samd.group2.tagapastry.messages.UntagMsg;
import dk.itu.samd.group2.tagapastry.messages.UpdateSizeMsg;
import rice.environment.logging.Logger;
import rice.p2p.commonapi.Message;
import rice.p2p.commonapi.NodeHandle;
import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.MessageDeserializer;
import rice.p2p.commonapi.rawserialization.RawMessage;
import rice.pastry.direct.DirectNodeHandle;
import rice.pastry.direct.GenericSimulatorListener;
import rice.pastry.leafset.BroadcastLeafSet;
import rice.pastry.routing.RouteMessage;

public class TagapastrySimulatorListener implements GenericSimulatorListener<DirectNodeHandle, RawMessage>
{
	private int delaySum;
	private int numOfDelays;
	
	public void TagapastrySimulatorListener() {
		this.delaySum = 0;
		this.numOfDelays = 0;
	}
	
	public float getAverageDelay() {
		return (float)(delaySum/numOfDelays);
	}
	
	public int getNumOfMsgsPassed() {
		return this.numOfDelays;
	}
	
	public void resetValues() {
		this.delaySum = 0;
		this.numOfDelays = 0;
	}
	
	@Override
	public void messageReceived(RawMessage arg0, DirectNodeHandle arg1,
			DirectNodeHandle arg2) {
		// TODO Auto-generated method stub		
	}

	@Override
	public void messageSent(RawMessage arg0, DirectNodeHandle arg1,
			DirectNodeHandle arg2, int arg3) {

		if(arg0.getType() == -23525) // Route message
		{
			RouteMessage rc = (RouteMessage)arg0;
			if(rc.getInternalType() == 2 || rc.getInternalType() == 3)
			{
				delaySum += arg3;
				numOfDelays++;
			}
		}
	}

}
